package com.nb.nbbase2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nb.nbbase2.entity.NsUser;
import com.nb.nbbase2.entity.SysArea;
import com.nb.nbbase2.exception.BizException;
import com.nb.nbbase2.mapper.NsUserMapper;
import com.nb.nbbase2.model.ResultEnum;
import com.nb.nbbase2.model.dto.LoginDTO;
import com.nb.nbbase2.model.dto.RegistryDTO;
import com.nb.nbbase2.model.vo.ProvinceCntVO;
import com.nb.nbbase2.model.vo.UserInfoVO;
import com.nb.nbbase2.service.INsUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nb.nbbase2.service.ISysAreaService;
import com.nb.nbbase2.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者
 * @since 2024-10-02
 */
@Slf4j
@Service
public class NsUserServiceImpl extends ServiceImpl<NsUserMapper, NsUser> implements INsUserService {

    @Autowired
    NsUserMapper nsUserMapper;

    @Autowired
    ISysAreaService sysAreaService;

    @Override
    public List<ProvinceCntVO> countByProvince() {
        return nsUserMapper.countByProvince();
    }

    @Override
    public UserInfoVO login(LoginDTO loginDTO) {
        LambdaQueryWrapper<NsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NsUser::getQq,loginDTO.getQq());
        NsUser one = getOne(wrapper);
        if(Objects.isNull(one)){
            throw new BizException(ResultEnum.LOGIN_ERROR);
        }

        if(!StringUtils.equals(one.getPassword(),loginDTO.getPassword())){
            throw new BizException(ResultEnum.LOGIN_ERROR);
        }
        String jwt = JwtUtil.generateToken(one.getQq());
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setQq(one.getQq());
        userInfoVO.setNickname(one.getNickname());
        userInfoVO.setJwt(jwt);

        return userInfoVO;
    }

    @Override
    public void registry(RegistryDTO registryDTO) {
        SysArea sysArea = sysAreaService.getById(registryDTO.getDistrictId());
        NsUser user = NsUser.builder()
                .qq(registryDTO.getQq())
                .nickname(registryDTO.getNickname())
                .password(registryDTO.getPassword())
                .provinceId(registryDTO.getProvinceId())
                .cityId(registryDTO.getCityId())
                .districtId(registryDTO.getDistrictId())
                .area(sysArea.getMergerName())
                .build();
        save(user);
    }

    @Override
    public boolean checkQqExist(String qq) {
        LambdaQueryWrapper<NsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NsUser::getQq,qq);
        NsUser nsUser = getOne(wrapper);
        return Objects.nonNull(nsUser);
    }

    @Override
    public NsUser getByQQ(String userId) {
        LambdaQueryWrapper<NsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NsUser::getQq,userId);
        return getOne(wrapper);
    }
}
